package com.service;

import com.dao.CommentDao;
import com.dao.CommentDaoImpl;
import com.dao.PostDetailDao;
import com.dao.PostDetailDaoImpl;
import com.model.Comment;
import com.model.PostDetail;
import com.util.DBUtil;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

public class PostDetailServiceImpl implements PostDetailService {
    private PostDetailDao postDetailDao = new PostDetailDaoImpl();
    private CommentDao commentDao = new CommentDaoImpl();

    @Override
    public PostDetail getPostDetail(int postId) throws SQLException {
        Connection conn = null;
        try {
            conn = DBUtil.getConnection();
            PostDetail post = postDetailDao.getPostById(conn, postId);
            if (post != null) {
                // 补充点赞数
                int likeCount = postDetailDao.getLikeCount(conn, postId);
                post.setLikeCount(likeCount);
            }
            return post;
        } finally {
            DBUtil.close(conn, null, null);
        }
    }

    @Override
    public List<Comment> getPostComments(int postId) throws SQLException {
        Connection conn = null;
        try {
            conn = DBUtil.getConnection();
            return commentDao.getCommentsByPostId(conn, postId);
        } finally {
            DBUtil.close(conn, null, null);
        }
    }

    @Override
    public int getCommentCount(int postId) throws SQLException {
        Connection conn = null;
        try {
            conn = DBUtil.getConnection();
            return commentDao.getCommentCount(conn, postId);
        } finally {
            DBUtil.close(conn, null, null);
        }
    }

    @Override
    public boolean toggleLike(int postId, int userId) throws SQLException {
        Connection conn = null;
        try {
            conn = DBUtil.getConnection();
            conn.setAutoCommit(false);

            boolean isLiked = postDetailDao.isUserLiked(conn, postId, userId);
            int result;

            if (isLiked) {
                // 取消点赞
                result = postDetailDao.cancelLike(conn, postId, userId);
            } else {
                // 新增点赞
                result = postDetailDao.addLike(conn, postId, userId);
            }

            conn.commit();
            return result > 0;
        } catch (SQLException e) {
            if (conn != null) conn.rollback();
            e.printStackTrace();
            return false;
        } finally {
            DBUtil.close(conn, null, null);
        }
    }

    @Override
    public boolean isUserLiked(int postId, int userId) throws SQLException {
        Connection conn = null;
        try {
            conn = DBUtil.getConnection();
            return postDetailDao.isUserLiked(conn, postId, userId);
        } finally {
            DBUtil.close(conn, null, null);
        }
    }

    @Override
    public boolean addComment(Comment comment) throws SQLException {
        Connection conn = null;
        try {
            conn = DBUtil.getConnection();
            conn.setAutoCommit(false);
            int result = commentDao.addComment(conn, comment);
            conn.commit();
            return result > 0;
        } catch (SQLException e) {
            if (conn != null) conn.rollback();
            e.printStackTrace();
            return false;
        } finally {
            DBUtil.close(conn, null, null);
        }
    }
}